package com.dianganjiao.lib.util

import android.app.Activity
import android.graphics.Bitmap
import android.os.Build
import android.view.ViewTreeObserver
import android.widget.ImageView
import androidx.annotation.RequiresApi
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.FitCenter
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.dianganjiao.lib.R
import com.dianganjiao.lib.xbanner.RoundCorners
import java.io.File

/**
 * create file time : 2021/3/1
 * create user : wxl
 * subscribe :
 */
class ImgLoader {

    companion object {


        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        private fun imageViewContextDestroyed(imageView: ImageView): Boolean {
            if (imageView.context is Activity) {
                return (imageView.context as Activity).isDestroyed
            }
            return false
        }

        /**
         * 加载资源图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadResouce(imageView: ImageView, resId: Int) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId).apply(RequestOptions.centerCropTransform())
                .into(imageView)
        }

        /**
         * 加载资源图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadResouceNoTransform(imageView: ImageView, resId: Int) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId)
                .into(imageView)
        }


        /**
         * 加载资源图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadResouce(imageView: ImageView, resId: Int, error: Int) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId)
                .apply(RequestOptions.centerCropTransform().error(error).placeholder(error))
                .into(imageView)
        }

        /**
         * 加载资源图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadScaledWHResource(imageView: ImageView, resId: Int) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).asBitmap().load(resId)
                .apply(RequestOptions.centerCropTransform())
                .into(object : SimpleTarget<Bitmap>() {
                    override fun onResourceReady(
                        resource: Bitmap,
                        transition: Transition<in Bitmap>?
                    ) {
                        imageView.viewTreeObserver.addOnGlobalLayoutListener(object :
                            ViewTreeObserver.OnGlobalLayoutListener {
                            override fun onGlobalLayout() {
                                imageView.viewTreeObserver.removeOnGlobalLayoutListener(this)
                                val c = resource.height.toFloat() / resource.width.toFloat()
                                imageView.layoutParams.height = (c * imageView.width).toInt()
                                Glide.with(imageView)
                                    .load(resource)
                                    .apply(RequestOptions.centerCropTransform())
                                    .into(imageView)
                            }

                        })
                    }

                })
        }

        /**
         * 加载资源图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadCircleCropResouce(imageView: ImageView, resId: Int) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId).apply(RequestOptions.circleCropTransform())
                .into(imageView)
        }

        /**
         * 加载资源图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadCornerResouce(imageView: ImageView, resId: Int, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId).apply(
                RequestOptions().transform(
                    CenterCrop(),
                    RoundCorners(corner, corner, corner, corner)
                )
            ).into(imageView)
        }


        /**
         * 加载资源图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadCornerResouceFix(imageView: ImageView, resId: Int, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId).apply(
                RequestOptions().transform(
                    FitCenter(),
                    RoundCorners(corner, corner, corner, corner)
                )
            ).into(imageView)
        }


        /**
         * 加载资源图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadCornerTopResouce(imageView: ImageView, resId: Int, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId).apply(
                RequestOptions().transform(
                    CenterCrop(),
                    RoundCorners(corner, corner, 0f, 0f)
                )
            ).into(imageView)
        }

        /**
         * 加载资源图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadCornerTopResouceFix(imageView: ImageView, resId: Int, width: Int, height:Int) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId)
                .override(width, height)
                .into(imageView)
        }

        /**
         * 加载资源图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadCornerBottomResouce(imageView: ImageView, resId: Int, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId).apply(
                RequestOptions().transform(
                    CenterCrop(),
                    RoundCorners(0f, 0f, corner, corner)
                )
            ).into(imageView)
        }

        /**
         * 加载资源图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadCornerBottom(imageView: ImageView, url: String, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(url).apply(
                RequestOptions().transform(
                    CenterCrop(),
                    RoundCorners(0f, 0f, corner, corner)
                )
            ).into(imageView)
        }


        /**
         * 加载资源图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadCornerLeftResouce(imageView: ImageView, resId: Int, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId).apply(
                RequestOptions().transform(
                    CenterCrop(),
                    RoundCorners(corner, 0f, 0f, corner)
                )
            ).into(imageView)
        }

        /**
         * 加载资源图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadCornerRightResouce(imageView: ImageView, resId: Int, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId).apply(
                RequestOptions().transform(
                    CenterCrop(),
                    RoundCorners(0f, corner, corner, 0f)
                )
            ).into(imageView)
        }

        /**
         * 加载网络图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun load(imageView: ImageView, url: String) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(url)
                .apply(RequestOptions.centerCropTransform()).into(imageView)
        }

        /**
         * 加载网络图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadfix(imageView: ImageView, url: String) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(url)
                .apply(RequestOptions.fitCenterTransform()).into(imageView)
        }

        /**
         * 加载网络图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun load(imageView: ImageView, url: String, error: Int) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(url)
                .apply(RequestOptions.centerCropTransform().error(error).placeholder(error))
                .into(imageView)
        }

        /**
         * 加载网络图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun file(imageView: ImageView, path: String) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(File(path))
                .apply(RequestOptions.centerCropTransform()).into(imageView)
        }

        /**
         * 加载网络图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadCircleCrop(imageView: ImageView, url: String) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).clear(imageView)
            Glide.with(imageView).load(url)
                .error(R.drawable.shape_circle_gray)
                .placeholder(R.drawable.shape_circle_gray)
                .apply(RequestOptions.circleCropTransform()).into(imageView)
        }

        /**
         * 加载本地图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadCornerFile(imageView: ImageView, path: String, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(File(path))
                .apply(
                    RequestOptions().transform(
                        CenterCrop(),
                        RoundCorners(corner, corner, corner, corner)
                    )
                ).into(imageView)
        }

        /**
         * 加载本地图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadCircleCropFile(imageView: ImageView, path: String) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(File(path))
                .apply(RequestOptions.circleCropTransform()).into(imageView)
        }


        /**
         * 加载资源图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadScaledWH(imageView: ImageView, url: String) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).asBitmap().load(url).apply(RequestOptions.centerCropTransform())
                .into(object : SimpleTarget<Bitmap>() {
                    override fun onResourceReady(
                        resource: Bitmap,
                        transition: Transition<in Bitmap>?
                    ) {
                        imageView.viewTreeObserver.addOnGlobalLayoutListener(object :
                            ViewTreeObserver.OnGlobalLayoutListener {
                            override fun onGlobalLayout() {
                                imageView.viewTreeObserver.removeOnGlobalLayoutListener(this)
                                val c = resource.height.toFloat() / resource.width.toFloat()
                                imageView.layoutParams.height = (c * imageView.width).toInt()
                                Glide.with(imageView)
                                    .load(resource)
                                    .apply(RequestOptions.centerCropTransform())
                                    .into(imageView)
                            }
                        })
                    }

                })
        }

        /**
         * 加载资源图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadScaledWHCorner(imageView: ImageView, url: String, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).asBitmap().load(url).apply(RequestOptions.centerCropTransform())
                .into(object : SimpleTarget<Bitmap>() {
                    override fun onResourceReady(
                        resource: Bitmap,
                        transition: Transition<in Bitmap>?
                    ) {
                        imageView.post {
                            val c = resource.height.toFloat() / resource.width.toFloat()
                            imageView.layoutParams.height = (c * imageView.width).toInt()
                            Glide.with(imageView)
                                .load(resource)
                                .apply(RequestOptions().transform(
                                    CenterCrop(),
                                    RoundCorners(corner, corner, corner, corner)
                                ))
                                .into(imageView)
                        }
                    }

                })
        }


        /**
         * 加载网络图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadCorner(imageView: ImageView, url: String, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(url)
                .apply(
                    RequestOptions().transform(
                        CenterCrop(),
                        RoundCorners(corner, corner, corner, corner)
                    )
                ).into(imageView)
        }


        /**
         * 加载网络图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadCornerTop(imageView: ImageView, url: String, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(url)
                .apply(
                    RequestOptions().transform(
                        CenterCrop(),
                        RoundCorners(corner, corner, 0f, 0f)
                    )
                ).into(imageView)
        }


        /**
         * 加载网络图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadCornerLeft(imageView: ImageView, url: String, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(url)
                .apply(
                    RequestOptions().transform(
                        CenterCrop(),
                        RoundCorners(corner, 0f, 0f, corner)
                    )
                ).into(imageView)
        }


        /**
         * 加载网络图片
         */
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        fun loadCornerRight(imageView: ImageView, url: String, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(url)
                .apply(
                    RequestOptions().transform(
                        CenterCrop(),
                        RoundCorners(0f, corner, corner, 0f)
                    )
                ).into(imageView)
        }



    }
}